package com.hw.one.collect.dust.job;

import com.hw.one.collect.client.ClientFactory;
import com.hw.one.collect.dust.constans.SjwConstants;
import com.hw.one.collect.dust.dao.RaiseDustDao;
import com.hw.one.collect.dust.po.RaiseDust;
import com.hw.one.collect.dust.po.SiteInfo;
import com.hw.one.collect.dust.redis.BaseRedis;
import com.hw.one.collect.mapper.SiteDao;
import com.hw.one.collect.param.SiteAirParam;
import com.hw.one.collect.po.SiteAirConcentrationPO;
import com.hw.one.collect.util.DateUtil;
import com.hw.one.collect.util.MathCalculateUtil;
import com.hw.one.collect.zzcity.constants.ZzCityConstants;
import com.mlb.analyse.webservice.DmDataInfoVO;
import com.mlb.analyse.webservice.GetPmData;
import com.mongodb.WriteResult;
import org.apache.cxf.endpoint.Client;
import org.apache.log4j.Logger;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/7.
 */
@Component
public class RaiseDustMissDataTest {

    private static Logger log = Logger.getLogger(RaiseDustMissDataTest.class);

    @Resource
    private BaseRedis baseRedis;

    @Resource
    private MongoTemplate mongoTemplateStandardSiteData;

    @Resource
    private SiteDao siteDao;

    @Resource
    private RaiseDustDao raiseDustDao;


    public void start() {

//        RaiseDust siteInfo = new RaiseDust();
//        siteInfo.setSITENAME("123");
//        siteInfo.setSITECODE("123456");
//        siteInfo.setMONITORTIME("2017-08-08 08:00:00");
//        baseRedis.addObjList("sjwRaiseDustMissData", siteInfo);
//        log.debug("----------------查询缺失记录----");
////        List<String> missDataList = baseRedis.getObjList("sjwRaiseDustMissData");
////        log.debug("------------本次查询：" + missDataList.size() + "个");
//        String[] s = ZzCityConstants.UCODE.split(",");
//        for (int i=0; i<s.length; i++) {
//            log.debug("-----------------现在查询：" + s[i]);
//            SiteAirParam siteAirParam = new SiteAirParam();
//            siteAirParam.setDataSources("webapi_zzs");
//            siteAirParam.setSiteCode(s[i]);
//            SiteAirConcentrationPO siteAirConcentrationPO = findSiteAirListByTimeAndDataSources(siteAirParam);
//            log.debug(siteAirConcentrationPO);
//
//        }
        try {
            log.info("--------开始测试redis超时时间测试---");
            log.info("--------首先插入一个永久数据---");
            baseRedis.insert("testTimeOut", "testTimeOut");
            String t1 = baseRedis.get("testTimeOut");
            log.info("--------查询数据---" + t1);
            baseRedis.insertWithTime("testTimeOut", "testTimeOut", 10);
            log.info("--------插入一个相同的数据，有效期10s---");
            Thread.sleep(5000);
            t1 = baseRedis.get("testTimeOut");
            log.info("--------5s后---" + t1);
            Thread.sleep(8000);
            t1 = baseRedis.get("testTimeOut");
            log.info("-------13秒后的数据：" + t1);

        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    private SiteAirConcentrationPO findSiteAirListByTimeAndDataSources(SiteAirParam siteAirParam) {
        // 查询类型不等于传入的类型
        Criteria criteria = Criteria.where("DATASOURCES").ne(siteAirParam.getDataSources());
        criteria.and("SITECODE").is(siteAirParam.getSiteCode());
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
//        criteria.and("MONITORTIME").gte(siteAirParam.getStartTime()).lte(siteAirParam.getEndTime());
        // 创建查询对象
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
        return mongoTemplateStandardSiteData.findOne(query,SiteAirConcentrationPO.class ,
                "T_DATA_AIR_MIN_CONCENTRATION-" + year);
    }



    public void updateData() {
        List<SiteInfo> siteInfoLists = siteDao.selectSiteInfoBySiteType("106");

        for (SiteInfo siteInfo : siteInfoLists) {
            String siteCode = siteInfo.getSiteCode();
            String siteName = siteInfo.getSiteName();
            // 根据名称更新编码
            Criteria criteria = Criteria.where("SITENAME").is(siteName);
            Query query = new Query(criteria);
            Update update = new Update();
            update.set("SITECODE", siteCode);
            WriteResult writeResult = mongoTemplateStandardSiteData.updateMulti(query, update, "T_DATA_RAISE_DUST_MIN-2017");

            mongoTemplateStandardSiteData.updateMulti(query, update, "T_DATA_RAISE_DUST_MIN");
            if (null != writeResult && writeResult.getN() > 0) {
                System.out.println(siteName + "【"+siteCode+"】查到" +writeResult.getN() + "记录");
                SiteInfo siteInfo1 = new SiteInfo();
                siteInfo1.setIsUse("1");
                siteInfo1.setSiteCode(siteCode);
                siteDao.updateSiteBySiteCode(siteInfo1);
            }else {
                System.out.println(siteName + "【"+siteCode+"】查到0记录");
            }
        }


    }
}
